// server/database/migrations/2025110604_create_suppliers_table.js
'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 创建供应商表
    await queryInterface.createTable('suppliers', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        unique: true,
        comment: '供应商名称'
      },
      contact_person: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '联系人'
      },
      phone: {
        type: Sequelize.STRING(20),
        allowNull: true,
        comment: '联系电话'
      },
      email: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '邮箱'
      },
      address: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: '地址'
      },
      remark: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注'
      },
      status: {
        type: Sequelize.STRING(20),
        defaultValue: 'pending',
        comment: '状态: pending-待审核, active-合作中, suspended-已暂停, terminated-已终止'
      },
      rating: {
        type: Sequelize.DECIMAL(3, 2),
        allowNull: true,
        defaultValue: 0.00,
        comment: '供应商评分'
      },
      cooperation_date: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '合作开始时间'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '创建时间'
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '更新时间'
      }
    });

    // 添加索引
    await queryInterface.addIndex('suppliers', ['name'], {
      name: 'suppliers_idx_name',
      unique: false
    });
  },

  down: async (queryInterface, Sequelize) => {
    // 删除索引
    await queryInterface.removeIndex('suppliers', 'suppliers_idx_name');
    
    // 删除表
    await queryInterface.dropTable('suppliers');
  }
};